package com.sunmouse.toolkit.widget.progress;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import com.sunmouse.toollib.utils.StringUtils;
import com.sunmouse.toolkit.R;

/**
 * Created by leiyun on 2018/5/23.
 */
public class LoadBar extends View {

    private Paint mStrokePaint;
    private Paint mFillPaint;
    private Paint mTxtPaint;
    private int mMax;
    private int mProgress;
    private int mStrokeColor;
    private int mFillColor;
    private int mTxtColor;
    private int mTxtSize;
    private float mCenterX;
    private float mCenterY;
    private float mRadius;
    private RectF mRect;
    private float mStrokeWidth;
    private float mTxtWidth;
    private float mTxtHeight;

    public LoadBar(Context context) {
        this(context, null, 0);
    }

    public LoadBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LoadBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.LoadBar);
        this.mStrokeColor = array.getColor(R.styleable.LoadBar_lbStrokeColor, Color.BLUE);
        this.mFillColor = array.getColor(R.styleable.LoadBar_lbFillColor, Color.GREEN);
        this.mTxtColor = array.getColor(R.styleable.LoadBar_lbTxtColor, Color.GRAY);
        this.mStrokeWidth = array.getDimensionPixelSize(R.styleable.LoadBar_lbStrokeWidth, 1);
        this.mTxtSize = array.getDimensionPixelSize(R.styleable.LoadBar_lbTxtSize, 32);
        array.recycle();

        //stroke paint
        this.mStrokePaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
        this.mStrokePaint.setColor(mStrokeColor);
        this.mStrokePaint.setStrokeWidth(mStrokeWidth);
        this.mStrokePaint.setStyle(Paint.Style.STROKE);
        //fill paint
        this.mFillPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
        this.mFillPaint.setColor(mFillColor);
        this.mFillPaint.setStyle(Paint.Style.FILL);

        //txt color
        this.mTxtPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
        this.mTxtPaint.setStyle(Paint.Style.FILL);
        this.mTxtPaint.setColor(mTxtColor);
        this.mTxtPaint.setTextSize(mTxtSize);

        this.mRect = new RectF();

        Paint.FontMetrics m = mTxtPaint.getFontMetrics();
        mTxtHeight = Math.abs(m.top / 2);
    }


    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

        mCenterX = w / 2;
        mCenterY = h / 2;
        float remainSize = w > h ? (h - getPaddingTop() - getPaddingBottom()) : (w - getPaddingLeft() - getPaddingRight());
        mRadius = remainSize / 2;

        mRect.left = getPaddingLeft() + mStrokeWidth;
        mRect.top = getPaddingTop() + mStrokeWidth;
        mRect.right = mRect.left + remainSize - 2 * mStrokeWidth;
        mRect.bottom = mRect.top + remainSize - 2 * mStrokeWidth;


    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        //draw stroke
        canvas.drawCircle(mCenterX, mCenterY, mRadius, mStrokePaint);
        //draw progress
        canvas.drawArc(mRect, 0, 360 * mProgress / mMax, true, mFillPaint);
        //draw text progress
        String p = mProgress * 100 / mMax + "%";
        mTxtWidth = StringUtils.getTextWidth(p, mTxtPaint);
        canvas.drawText(p, mCenterX - mTxtWidth / 2.0f, mCenterY + mTxtHeight / 2.0f, mTxtPaint);
    }

    /**
     * set progress
     *
     * @param mProgress
     */
    public void setProgress(int mProgress) {
        this.mProgress = mProgress;
        postInvalidate();
    }

    /**
     * set max value
     *
     * @param mMax
     */
    public void setMax(int mMax) {
        this.mMax = mMax;
        postInvalidate();
    }
}
